//
//  AYStatusOriginalView.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/18.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit
import SDWebImage

class AYStatusOriginalView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.yellowColor()
        
        
        preparUI()
    }
    
    ///原创微博需要的内容
    var status:AYStatus? {
        didSet {
            if let urlString = status?.user?.profile_image_url {
                let url = NSURL(string: urlString)
                
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))

            }
            nameLabel.text = status?.user?.screen_name
            
            timeLabel.text = status?.created_at
            
            sourceLabel.text = status?.source
            
            memberView.image = status?.user?.mbrankImage
            
            verifiedView.image = status?.user?.verifiedImage
            
            contentLabel.text = status?.text
            
            pictureView.pictureURLs = status?.pictureURLs
            
            ///更新约束
            pictureView.snp_updateConstraints { (make) in
                let margin = status?.pictureURLs?.count == 0 ? 0 : CellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    
    private func preparUI() {
        backgroundColor = UIColor.whiteColor()

        ///添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        ///添加约束
        
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 会员等级图标
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        ///文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
            
        }
        ///原创配图View
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            make.width.height.equalTo(359)
        }
        
        ///设置原创View的底部
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
        
    }
    

    
    
    // MARK: - lazy
    
    ///头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    ///用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(NameFontSize)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    ///时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        

        label.textColor = UIColor.orangeColor()
        return label
    }()
    
    ///来源
    private lazy var sourceLabel: UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    ///会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    
    ///认证类型
    private lazy var verifiedView: UIImageView = UIImageView(image: nil)
    
    ///微博文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - CellMargin * 2
        return label
    }()
    ///微博原创配图
     private lazy var pictureView:AYStatusPictureView = AYStatusPictureView()
}



